package com.ufo.store.management.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.ufo.store.management.R;

import java.util.ArrayList;

/**
 * Created by taoli on 2016/7/20.
 */
public class StockTypeGridListAdapter extends BaseAdapter {
    private Context context = null;
    private ArrayList<String> typeList = new ArrayList<>();
    private int mDefault = 0;

    public static class ViewHolder {
        public TextView typeTv;
    }

    public StockTypeGridListAdapter(Context context) {
        this.context = context;

    }

    public void addItem(String item) {
        typeList.add(item);
    }

    public void removeItem(String item) {
        for (int i = 0; i < typeList.size(); i++) {
            if (item == typeList.get(i)) {
                typeList.remove(i);
            }
        }
    }

    public void clearItem() {
        typeList.clear();
    }

    public void setDefault(int position){
        mDefault = position;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return typeList.size();
    }

    @Override
    public String getItem(int position) {
        String item = null;
        if (position >= 0 && getCount() > position) {
            item = typeList.get(position);
        }
        return item;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (getCount() == 0) {
            return null;
        }

        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(this.context).inflate(R.layout.item_stocktype_grid, null);
            viewHolder.typeTv = (TextView) convertView.findViewById(R.id.tv_type);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        String s = typeList.get(position);
        if( !TextUtils.isEmpty(s) ){
            viewHolder.typeTv.setText(s.trim());
        }
        if(mDefault == position){
            viewHolder.typeTv.setBackgroundResource(R.drawable.shape_btn_check);
            viewHolder.typeTv.setTextColor(context.getResources().getColor(R.color.app_key_color));
        }else{
            viewHolder.typeTv.setBackgroundColor(context.getResources().getColor(R.color.app_bar_color));
            viewHolder.typeTv.setTextColor(0xff8c8c8c);
        }
        return convertView;
    }
}

